package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserAccount;
import com.ruoyi.system.domain.WebUserWithdraw;
import com.ruoyi.system.mapper.WebUserWithdrawMapper;
import com.ruoyi.system.service.IWebUserAccountService;
import com.ruoyi.system.service.IWebUserService;
import com.ruoyi.system.service.IWebUserWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用户提现申请Service业务层处理
 * 
 * @author wsliupan@126.com
 * @date 2021-06-06
 */
@Service
public class WebUserWithdrawServiceImpl extends ServiceImpl<WebUserWithdrawMapper, WebUserWithdraw> implements IWebUserWithdrawService
{
    @Autowired
    private WebUserWithdrawMapper webUserWithdrawMapper;

    /**
     * 查询用户提现申请
     * 
     * @param id 用户提现申请ID
     * @return 用户提现申请
     */
    @Override
    public WebUserWithdraw selectWebUserWithdrawById(Long id)
    {
        return webUserWithdrawMapper.selectWebUserWithdrawById(id);
    }

    /**
     * 查询用户提现申请列表
     * 
     * @param webUserWithdraw 用户提现申请
     * @return 用户提现申请
     */
    @Override
    public List<WebUserWithdraw> selectWebUserWithdrawList(WebUserWithdraw webUserWithdraw)
    {
        return webUserWithdrawMapper.selectWebUserWithdrawList(webUserWithdraw);
    }

    /**
     * 新增用户提现申请
     * 
     * @param webUserWithdraw 用户提现申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWebUserWithdraw(WebUserWithdraw webUserWithdraw)
    {
        webUserWithdraw.setCreateTime(DateUtils.getNowDate());
        int rows = webUserWithdrawMapper.insertWebUserWithdraw(webUserWithdraw);
        insertWebUser(webUserWithdraw);
        return rows;
    }

    /**
     * 修改用户提现申请
     * 
     * @param webUserWithdraw 用户提现申请
     * @return 结果
     */
    @Transactional
    @Override
    public boolean updateWebUserWithdraw(WebUserWithdraw webUserWithdraw)
    {
        //webUserWithdraw.setUpdateTime(DateUtils.getNowDate());
        webUserWithdrawMapper.deleteWebUserById(webUserWithdraw.getId());
        insertWebUser(webUserWithdraw);
        //return webUserWithdrawMapper.updateWebUserWithdraw(webUserWithdraw);
        return this.saveOrUpdate(webUserWithdraw);
    }

    /**
     * 删除用户提现申请对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteWebUserWithdrawByIds(String ids)
    {
        webUserWithdrawMapper.deleteWebUserByIds(Convert.toStrArray(ids));
        //return webUserWithdrawMapper.deleteWebUserWithdrawByIds(Convert.toStrArray(ids));
        return this.removeByIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 删除用户提现申请信息
     * 
     * @param id 用户提现申请ID
     * @return 结果
     */
    @Override
    public boolean deleteWebUserWithdrawById(Long id)
    {
        webUserWithdrawMapper.deleteWebUserById(id);
        //return webUserWithdrawMapper.deleteWebUserWithdrawById(id);
        return this.removeById(id);
    }

    /**
     * 新增用户信息
     * 
     * @param webUserWithdraw 用户提现申请对象
     */
    public void insertWebUser(WebUserWithdraw webUserWithdraw)
    {
        List<WebUser> webUserList = null;//webUserWithdraw.getWebUserList();
        Long id = webUserWithdraw.getId();
        if (StringUtils.isNotNull(webUserList))
        {
            List<WebUser> list = new ArrayList<WebUser>();
            for (WebUser webUser : webUserList)
            {
                webUser.setId(id);
                list.add(webUser);
            }
            if (list.size() > 0)
            {
                webUserWithdrawMapper.batchWebUser(list);
            }
        }
    }


    @Transactional
    public boolean saveWithdraw(WebUserWithdraw webUserWithdraw , WebUser userData){
        WebUserAccount account = iWebUserAccountService.getOne(new QueryWrapper<WebUserAccount>().eq("user_id",webUserWithdraw.getUserId()));
        if(account == null){
            throw new BusinessException("抱歉，请先绑定银行卡才能进行提现!");
        }
        if(StringUtils.isEmpty(account.getAlipayNo())){
            throw new BusinessException("您当前没有绑定支付宝账号");
        }
        webUserWithdraw.setIdcard(account.getIdcard());
        webUserWithdraw.setBankType(account.getBankType());
        webUserWithdraw.setBindType(account.getBindType());
        webUserWithdraw.setBankNo(account.getBankNo());
        webUserWithdraw.setAlipayNo(account.getAlipayNo());
        webUserWithdraw.setStatus(0); //待打款
        webUserWithdraw.setAmount(webUserWithdraw.getAmount() * 100); //分
        webUserWithdraw.setCurrentTotal(userData.getEarnings());
        save(webUserWithdraw);

        //减余额
        int earnings = userData.getEarnings();
        earnings = earnings - (Integer.valueOf(webUserWithdraw.getAmount()+""));
        if(earnings < 0){
            throw new BusinessException("提现余额不足");
        }
        userData.setEarnings(earnings);
        iWebUserService.update(new UpdateWrapper<WebUser>().eq("id", userData.getId()).set("earnings",earnings));

        return true;
    }


    @Autowired
    private IWebUserAccountService iWebUserAccountService;
    @Autowired
    private IWebUserService iWebUserService;
}
